package com.first.wchat.connect.udp;

import com.first.wchat.data.Constant;
import com.first.wchat.data.MSGDataManager;
import com.first.wchat.util.ScanUtil;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;

public class UDPConnectSevice extends Service implements DataSendListener,DataReceiverListener{

	UDPConnectSeviceBinder mBinder;
	
	
	UDPSender mSender;
	UDPRecevier mRecevier;
	public class UDPConnectSeviceBinder extends Binder { 
		public UDPConnectSevice getService() { 
                return UDPConnectSevice.this; 
        } 
	} 

	@Override
	public void onCreate() 
	{
		super.onCreate();
		mBinder = new UDPConnectSeviceBinder();
		mSender = new UDPSender();
		mRecevier = new UDPRecevier();
		mSender.setListener(this);
		mRecevier.setListener(this);
		mRecevier.receive(this);
	}
	
	
	@Override
	public IBinder onBind(Intent arg0) {
		
		return mBinder;
	}

	
	public void send(byte[] data, String name)
	{
		String text = new String(data);
		mSender.send(this, text,name);
		//MSGDataManager.getInstance().addLocalMSG(name, text, null);
	}


	@Override
	public void receiver(String ip,String msg,String date) {
		Intent intent = new Intent(Constant.ACTION_RECEIVE);
		Bundle b = new Bundle();
		b.putString(Constant.DATA, msg);
		b.putString(Constant.IP, ip);
		b.putString(Constant.DATE, date);
		intent.putExtras(b);
		sendBroadcast(intent);
		
		//MSGDataManager.getInstance().addReceivedMSG(ip, msg, date);
	}


	@Override
	public void send(String data) {
		Intent intent = new Intent(Constant.ACTION_SEND);
		Bundle b = new Bundle();
		b.putString(Constant.DATA, data);
		intent.putExtras(b);
		sendBroadcast(intent);
		
	}
	
	

}
